<?php

namespace App\Admin\Controllers;

use App\UserSign;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class UserSignController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '签到领奖记录';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserSign());
        $grid->model()->orderBy('id', 'desc');

        $grid->filter(function($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 设置datetime类型
            $filter->equal('id', '编号');
            $filter->like('user.name', '买家名称');
            $filter->like('user.phone', '买家手机号');
            $filter->between('updated_at', '领取时间')->datetime();


        });
        $grid->model()->where('status', 1);

        $grid->column('id', '编号');
        $grid->column('user.name', '用户名')->display(function($value){
            return $value.'<br/>'.$this->user->phone;
        });
        $grid->column('lot_type', '奖品类型')->display(function($value){
            return ($value == 1 ? '品鉴金':'vip')."+".$this->lot_value;
        });
        $grid->column('updated_at', '领取时间');
        $grid->disableActions();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserSign::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('uid', __('Uid'));
        $show->field('date', __('Date'));
        $show->field('count', __('Count'));
        $show->field('lot_type', __('Lot type'));
        $show->field('lot_value', __('Lot value'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserSign());

        $form->number('uid', __('Uid'));
        $form->date('date', __('Date'))->default(date('Y-m-d'));
        $form->number('count', __('Count'));
        $form->switch('lot_type', __('Lot type'))->default(1);
        $form->text('lot_value', __('Lot value'));
        $form->switch('status', __('Status'))->default(1);

        return $form;
    }
}
